<template>
  <div>
    <div class="search" v-if="user.role !== 'USER'">
      <el-input placeholder="请输入姓名查询" style="width: 200px" v-model="employeeName"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
      <el-button type="info" plain style="margin-left: 10px" @click="selectAllEmployee">查询所有</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="exportExcel" v-if="user.role === 'GENERAL_MANAGER'">批量导出</el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" stripe  @selection-change="handleSelectionChange">
        <el-table-column type="index" :index="hIndex" label="序号" ></el-table-column>
        <el-table-column prop="employeeName" label="姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="departmentName" label="所属部门" show-overflow-tooltip></el-table-column>
        <el-table-column prop="date" label="日期" show-overflow-tooltip></el-table-column>
        <el-table-column prop="startTime" label="上班打卡时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="endTime" label="下班打卡时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="isLate" label="是否迟到"></el-table-column>
        <el-table-column prop="lateTime" label="迟到时间"></el-table-column>

        <el-table-column prop="isEarly" label="是否早退"></el-table-column>
        <el-table-column prop="earlyTime" label="早退时间"></el-table-column>

        <el-table-column prop="leaveStatus" label="状态"></el-table-column>

        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="clockIn(scope.row)" size="mini" v-if="scope.row.startTime === null && scope.row.leaveStatus === '正常' && user.id === scope.row.employeeId && scope.row.date === currentDate"  >上班打卡</el-button>
            <el-button plain type="primary" @click="clockOut(scope.row)" size="mini" v-if="scope.row.endTime === null && scope.row.startTime !== null && scope.row.leaveStatus === '正常'  && user.id === scope.row.employeeId && scope.row.date === currentDate">下班打卡</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>


  </div>
</template>

<script>
export default {
  name: "AttendanceRecords",
  data() {
    return {
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      title: null,
      fromVisible: false,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      employeeName : null,
      isQueryAll : false,
      currentDate: new Date().toISOString().slice(0, 10)
    }
  },
  created() {
    this.load(1)
  },
  methods: {
    selectAllEmployee(){
      this.isQueryAll = true;
      this.load(1)
    },
    exportExcel(){
      window.open(this.$baseUrl + '/attendanceRecords/export')
    },
    clockIn(row){
      this.$request.get('/attendanceRecords/clockIn/' + row.id).then(res => {
        if (res.code === '200') {   // 表示操作成功
          this.$message.success('打卡成功')
          this.load(1)
        } else {
          this.$message.error(res.msg)  // 弹出错误的信息
        }
      })
    },
    clockOut(row){
      this.$request.get('/attendanceRecords/clockOut/' + row.id).then(res => {
        if (res.code === '200') {   // 表示操作成功
          this.$message.success('打卡成功')
          this.load(1)
        } else {
          this.$message.error(res.msg)  // 弹出错误的信息
        }
      })
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/attendanceRecords/update' : '/attendanceRecords/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/attendanceRecords/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)   //  [1,2]
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/attendanceRecords/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/attendanceRecords/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          employeeName: this.employeeName,
          isQueryAll: this.isQueryAll,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    reset() {
      this.employeeName = null
      this.isQueryAll = false;
      this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    // 当前页数 - 1 * 每页数据条数 + index + 1 （ index 是索引值，从0开始）
    hIndex(index) {
      return (this.pageNum - 1) * this.pageSize + index + 1;
    }
  },
}
</script>

<style scoped>

</style>
